package com.dayuanit.booksystem.v2.dao.impl;

import com.dayuanit.booksystem.v2.dao.BookDao;
import com.dayuanit.booksystem.v2.domain.PrepareParamer;
import com.dayuanit.booksystem.v2.entity.Book;

import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl extends BaseDaoImpl implements BookDao {
    @Override
    public int add(String bookName, String author, String price, long publisherId) {
        String sql = "insert into book(book_name, author, price, publisher_id) values(?, ?, ?, ?)";

        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, bookName));
        list.add(new PrepareParamer(String.class, author));
        list.add(new PrepareParamer(String.class, price));
        list.add(new PrepareParamer(long.class, publisherId));

        return modify(sql, list);
    }

    @Override
    public Book getBookById(long id) {
        String sql = "SELECT book_id, book_name, author, price, publisher_id " +
                "from book where book_id=?;";

        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(long.class, id));
        return commonQuery(sql, Book.class, bookMap, list);
    }

    @Override
    public Book getBookByName(String bookName) {
        String sql = "SELECT book_id, book_name, author, price, publisher_id from book where book_name=?;";
        List<PrepareParamer> list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, bookName));
        return commonQuery(sql, Book.class, bookMap, list);
    }

    /**
     * PreparedStatement sql预编译，能够解决sql注入的问题
     * @param bookName
     * @param price
     * @return
     */
    @Override
    public int updateBookPriceByName(String bookName, String price) {
        String sql = "UPDATE book set price=? where book_name=?";

        List list = new ArrayList<>();
        //不能用hashset,它是无序的
//        List list = new HashSet<>();

        list.add(new PrepareParamer(String.class, price));
        list.add(new PrepareParamer(String.class, bookName));
        return modify(sql, list);
    }

    @Override
    public List<Book> listBook(long publisherId) {
        String sql = "SELECT book_id as id, book_name as bookName, author as author, price as price, publisher_id as publisherId from book where publisher_id=?;";

        List list = new ArrayList<>();
        list.add(new PrepareParamer(long.class, publisherId));
        return commonQueryList(sql, Book.class, bookMap, list);
    }

    @Override
    public int count(long publisherId) {
        String sql = "SELECT count(*) from book WHERE publisher_id=?;";
        List list = new ArrayList<>();
        list.add(new PrepareParamer(long.class, publisherId));

        return commonCount(sql, list);
    }

    @Override
    public int updateBookPriceById(long bookId, String price) {
        String sql = "UPDATE book set price=? where book_id=?;";
        List list = new ArrayList<>();
        list.add(new PrepareParamer(String.class, price));
        list.add(new PrepareParamer(long.class, bookId));

        return modify(sql, list);
    }
}
